<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/6 0006
 * Time: 17:11
 */
namespace app\modules\content\models;

use app\modules\cn\models\ResourcesPhone;
use yii\db\ActiveRecord;

class Active extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%active}}';
    }

    public function rules()
    {
        return [
            // username and password are both required
            [['student', 'cate', '', 'duration'], 'required'],

        ];
    }

    public static function getReservation($data)
    {
        foreach ($data as $k => $v) {
            $count = ResourcesPhone::find()->asArray()->where('remark="' . $v['name'].'"')->count();
            $data[$k]['count'] = $v['reservations']+$count;
        }
        return $data;
    }

    public static function getActive($cate,$limit){
        $data = Active::find()->where("cate=$cate")->orderBy('id desc')->limit($limit)->asArray()->all();
        //老师
        foreach ($data as $k=>$v){
            $name = addslashes(trim($v['teacher']));
            $teacher = [];
            if($name){
                $teacher = Teachers::find()->where("name='$name'")->asArray()->one();
            }
            if(empty($teacher['pic'])){
                $data[$k]['teacherImg'] = \Yii::$app->params['defaultImg'];
            }else{
                $data[$k]['teacherImg'] = \Yii::$app->params['thinkUrl'].$teacher['pic'];
            }
            $data[$k]['course_tag'] = $v['course_tag']?explode(',',$v['course_tag']):null;
        }
        return $data;
    }
}